using System;
using System.Collections.Generic;
using Framework.Repository.Conventions;

namespace Framework.Repository.Testing
{
    public sealed class AutoMappingTypeProvider : IAutoMappingTypeProvider
    {
        private readonly HashSet<Type> entityTypes = new HashSet<Type>();
        private readonly HashSet<Type> subClassTypes = new HashSet<Type>();
        private readonly HashSet<Type> overrideTypes = new HashSet<Type>();

        private static void AddIfNeeded<T>(Func<Type, bool> func, ISet<Type> types)
        {
            var type = typeof(T);

            if (func(type))
            {
                types.Add(type);
            }
        }

        public AutoMappingTypeProvider AddEntity<T>()
        {
            AddIfNeeded<T>(AutoMappingHelper.IsEntity, entityTypes);
            AddIfNeeded<T>(AutoMappingHelper.IsSubClass, subClassTypes);
            AddIfNeeded<T>(AutoMappingHelper.IsAutoMappingOverrideType, overrideTypes);

            return this;
        }

        public IEnumerable<Type> GetEntityTypes()
        {
            return entityTypes;
        }

        public IEnumerable<Type> GetSubClassTypes()
        {
            return subClassTypes;
        }

        public IEnumerable<Type> GetOverrideTypes()
        {
            return overrideTypes;
        }
    }
}